package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;

import objects.Board;
import objects.ConfigurationType;
import objects.Player;

/**
 * Servlet implementation class EndGameServlet
 */
@WebServlet(name = "EndGame", urlPatterns = {"/EndGame"})
public class EndGameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EndGameServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
    public void Joiner(String joinedPlayer)
	{
		Player joinedInstancePlayer = null;
		
		//if (Board.getInstance().getBdGameData().getType().equals(ConfigurationType.MANUAL))
		//{
			joinedInstancePlayer = Board.getInstance().getBdGameData().getPlayerByIndex(Board.getInstance().getNumberOfPlayersJoined());
		//}
			
		joinedInstancePlayer.setPlayerName(joinedPlayer);
		joinedInstancePlayer.setOccupied(true);
		Board.getInstance().setNumberOfPlayersJoined(Board.getInstance().getNumberOfPlayersJoined() + 1);
		
		int nWait = 0;
		if (Board.getInstance().getNumberOfPlayersJoined() < Board.getInstance().getBdGameData().getPlayers().size())
		{
			nWait = 1;
		}
	
		if (nWait == 0)
		{
			//Board.getInstance().resetActions();
		}
		
		JsonObject jsAction = new JsonObject();
		jsAction.addProperty("code", "join");
		jsAction.addProperty("player", joinedPlayer);
		
		jsAction.addProperty("waitformore", nWait);
		jsAction.addProperty("message", "Player " +joinedPlayer+ " has joined the game!");
		Board.getInstance().addJsActions(jsAction);
		
		// Call index.jsp (the page to call was set by pageName on the jsp)
	}
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if (request.getParameter("select").equals("restart"))
		{
			String strPl = request.getParameter("browserPlayer");
			request.setAttribute("browserPlayer",strPl);
			JoinServlet.joined = new ArrayList<String>();
			Board.restart();
				
			//Board.getInstance().getBdGameData().getPlayers().addAll(compPlayers);
			JsonObject jsAction = new JsonObject();
			jsAction.addProperty("code", "restart");
			jsAction.addProperty("player", strPl);
			Board.getInstance().addJsActions(jsAction);
			for (Player player : Board.getInstance().getBdGameData().getPlayers())
			{
				Joiner(player.getPlayerName());
			}
			request.setAttribute("pageName", "game"); 
		}
		else
		{
			Board.Nullify();
			request.setAttribute("pageName", "newGame"); 
		}
		
    	// Call index.jsp (the page to call was set by pageName on the jsp)
    	RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
    	dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		//Board.getInstance().resetActions();
	}
}
